package com.example.yyj.tool;

import com.example.yyj.tool.BaiDu.OCR.OCRAuthService;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.example.yyj.tool.BaiDu.OCR.HttpUtil;
import com.example.yyj.tool.BaiDu.OCR.Base64Util;
import com.example.yyj.tool.BaiDu.OCR.FileUtil;
import com.example.yyj.tool.BaiDu.OCR.OCRAuthService;


import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class OCRUtil {
    public  String animal(String filePath) {//动物识别。
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal";
        OCRAuthService ocrAuthService = new OCRAuthService();//新建校验对象
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath); //以比特流形式上传图片
            String imgStr = Base64Util.encode(imgData);//转为Base64
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            String accessToken = ocrAuthService.getAuth(2);//获得Token
            String result = HttpUtil.post(url, accessToken, param);//Post形式上传
            JsonParser jp = new JsonParser();//新建JSON对象
            JsonObject jo = jp.parse(result).getAsJsonObject();//获取返回值对象
            String temp = jo.get("result").getAsJsonArray().get(0).getAsJsonObject().get("name").toString();
            temp=temp.substring(1,temp.length()-1);//获得结果
            System.out.println(temp);
            return temp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 基础图像识别、车牌识别、身份证识别
     * @param Path 图片路径
     * @param Type 识别类型
     * @return Map
     */
    public Map<String,String>BasicOCR(String Path,int Type){
        Map<String,String> map = new HashMap<>();//储存返回结果
        try{
            String url="";//请求地址
            String param="";//请求参数
            String temp="";
            byte [] imgData=FileUtil.readFileByBytes(Path);//以比特流的形式读入图片
            String imgStr=Base64Util.encode(imgData);//将比特流转为Base64
            String imgParam = URLEncoder.encode(imgStr,"UTF-8");//设置字符集格式为UTF-8
            if (Type == 1) { //根据类型 设置请求地址及参数的格式
                url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
                param = "image=" + imgParam;
            } else if (Type == 2) {
                url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
                param = "id_card_side=" + "front" + "&image=" + imgParam;
            } else {
                url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
                param = "image=" + imgParam;
            }
            OCRAuthService ocrAuthService = new OCRAuthService();//新建校验对象
            String result = HttpUtil.post(url,ocrAuthService.getAuth(1),param);//发起post请求并返回结果
            JsonParser jsonParser = new JsonParser();
            JsonObject jsonObject = jsonParser.parse(result).getAsJsonObject();//将结果转为JSON对象
            if (Type==3){//车牌识别结果解析
                temp=jsonObject.get("words_result").getAsJsonObject().get("number").toString();//带引号的结果
                map.put("车牌",temp.substring(1,temp.length()-1));
            }else if(Type==2){//身份证识别解析
                String [] par= new String[]{"姓名","民族","住址","公民身份号码","出生","性别"};
                for (String s:par){
                    temp=jsonObject.get("words_result").getAsJsonObject().getAsJsonObject(s).get("words").toString();
                    map.put(s,temp.substring(1,temp.length()-1));//去掉引号，并返回
                }
            }else{//通用识别
                String tmp=jsonObject.get("words_result").getAsJsonArray().get(0).toString();
                map.put("结果",tmp.substring(10,tmp.length()-2));
            }
            System.out.println("输出结果"+temp);
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
